/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package repository;

/**
 *
 * @author SHUtami
 */

//imported class
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FileController {
    public ResultSet AmbilVersiTerbaru(String Filename){
        try {
            DatabaseController DBCon = new DatabaseController();
            DBCon.ConnecttoDB();
            Statement stmt = null;
            ResultSet rs = null;
            
            //AMBIL Versi
            String SQL="SELECT max(version) as Ver FROM FILE WHERE filename='"+Filename+"'";
            stmt=DBCon.konek.createStatement();
            rs=stmt.executeQuery(SQL);
            rs.next();
            int Version = Integer.parseInt(rs.getString("Ver"));

            //AMBIL FILE
            SQL="SELECT * FROM file WHERE version="+Version+" AND filename='"+Filename+"'";
            stmt=DBCon.konek.createStatement();
            rs=stmt.executeQuery(SQL);

            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(PenggunaController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public ResultSet AmbilVersiTertentu(String Filename, int Version){
        try {
            DatabaseController DBCon = new DatabaseController();
            DBCon.ConnecttoDB();
            Statement stmt = null;
            ResultSet rs = null;

            //AMBIL FILE
            String SQL="SELECT * FROM file WHERE version="+Version+" AND filename='"+Filename+"'";
            stmt=DBCon.konek.createStatement();
            rs=stmt.executeQuery(SQL);

            return rs;
        } catch (SQLException ex) {
            Logger.getLogger(PenggunaController.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public void TambahFile(String Filename, int Version, String Location){
        try {
            DatabaseController DBCon = new DatabaseController();
            DBCon.ConnecttoDB();
            Statement stmt = null;
            ResultSet rs = null;

            //AMBIL FILE
            String SQL="SELECT * FROM file WHERE filename='"+Filename+"'";
            stmt=DBCon.konek.createStatement();
            rs=stmt.executeQuery(SQL);
            rs.next();
            int owner = Integer.parseInt(rs.getString("owner"));

            //Memasukkan ke Tabel File
            SQL="INSERT INTO file(filename, owner, version, serverlocation) VALUES ('"+Filename+"',"+owner+","+Version+",'"+Location+"')";
            stmt = DBCon.konek.createStatement();
            stmt.executeUpdate(SQL);
        } catch (SQLException ex) {
            Logger.getLogger(PenggunaController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void TambahFileOtomatis(String Filename){
        try {
            DatabaseController DBCon = new DatabaseController();
            DBCon.ConnecttoDB();
            Statement stmt = null;
            ResultSet rs = null;

            //AMBIL FILE
            rs = AmbilVersiTerbaru(Filename);
            rs.next();
            int owner=Integer.parseInt(rs.getString("owner"));
            int Version=Integer.parseInt(rs.getString("version"))+1;
            String Location=Filename+"/"+Version;


            //Memasukkan ke Tabel File
            String SQL="INSERT INTO file(filename, owner, version, serverlocation) VALUES ('"+Filename+"',"+owner+","+Version+",'"+Location+"')";
            stmt = DBCon.konek.createStatement();
            stmt.executeUpdate(SQL);
        } catch (SQLException ex) {
            Logger.getLogger(PenggunaController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
